package com.sdk.demo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.kg.sdk.KPGameListener;
import com.kg.sdk.KPGameSdk;
import com.kg.sdk.KPUserData;
import com.kg.sdk.dlg.KPPermissionDlg;
import com.kg.sdk.util.CommonUtil;
import com.kg.sdk.util.KPNetworkData;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

public class GameActivity extends Activity implements View.OnClickListener {
    private Button btn_game_exit;
    private Button btn_game_logout;
    private Button btn_game_login;
    private Button btn_game_pay;
    private Button btn_game_upinfo;
    private TextView text_message;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.act_game_main);
        btn_game_exit = (Button)findViewById(R.id.btn_game_exit);
        btn_game_logout = (Button)findViewById(R.id.btn_game_logout);
        btn_game_login = (Button)findViewById(R.id.btn_game_login);
        text_message = (TextView)findViewById(R.id.text_message);
        btn_game_pay = (Button)findViewById(R.id.btn_game_pay);
        btn_game_upinfo =(Button)findViewById(R.id.btn_game_upinfo);
        btn_game_pay.setOnClickListener(this);
        btn_game_exit.setOnClickListener(this);
        btn_game_logout.setOnClickListener(this);
        btn_game_login.setOnClickListener(this);
        text_message.setOnClickListener(this);
        btn_game_upinfo.setOnClickListener(this);
       // KPGameSdk.checkFloatPermission(this);
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        Log.e("se"," width is "+width+" height is "+height);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.btn_game_exit){//退出游戏
          //  KPGameSdk.hideFloatView(this);
            finish();
        }else if(id == R.id.btn_game_login){
            KPGameSdk.startLogin(GameActivity.this);//登录
        }else if(id == R.id.btn_game_logout){
            KPGameSdk.exitLogin(GameActivity.this);//登出
        }else if(id == R.id.text_message){
            KPGameSdk.startUserCenter(GameActivity.this);//启动用户中心
        }else if(id == R.id.btn_game_pay){
            /**
             *  try {
             *             jsonObject.put("ordersn","2017071017283610159");
             *             jsonObject.put("product","首充");
             *             jsonObject.put("role_id","1000");
             *             jsonObject.put("amount","6");
             *             jsonObject.put("server_id","server001");
             *         } catch (JSONException e) {
             *             e.printStackTrace();
             *         }
             */
            charge();
        }else if(id == R.id.btn_game_upinfo){
            KPUserData kpUserData = KPGameSdk.getKPUserData(GameActivity.this);
            if(kpUserData != null){
                StringBuilder builder = new StringBuilder();
                String split = " ";
                builder.append("auth_code: ");
                builder.append(kpUserData.auth_code);
                builder.append(split);
                builder.append("auth_key: ");
                builder.append(kpUserData.auth_key);
                builder.append(split);
                builder.append("use_code: ");
                builder.append(kpUserData.use_code);
                builder.append(split);
                builder.append("game_id: ");
                builder.append(kpUserData.game_id);
                builder.append(split);
                builder.append("channel_id: ");
                builder.append(kpUserData.channel_id);
                Log.e("kp"," getKPUserData is "+builder.toString());
                CommonUtil.showToast(GameActivity.this,builder.toString());
            }else{
                CommonUtil.showToast(GameActivity.this,"请先登录");
            }

        }
    }

    private void charge(){
        KPGameListener listener = new KPGameListener() {
            @Override
            public void onSuccess(JSONObject successJson) {
                /*
                         successJson,成功后返回的jsno
                        "orderno": "D19092407400000000264658",
                        "amount": "6",
                        "product": "首充",
                         "pay_status": 3 支付状态：1、已支付
                 */
                Log.e("ch"," charge success "+successJson.toString());
                try {
                    int pay_status = successJson.getInt("pay_status");
                    if(pay_status == 1){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                CommonUtil.showToast(GameActivity.this,"充值成功");
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(int style, String error) {
                Log.e("charge"," charge style is "+style);
                if(style == KPGameSdk.LOGIN_ERROR){//登录失效

                }else {//出错
                    //error为出错的信息
                }
            }
        };
        KPGameSdk.charge(GameActivity.this,"2017071017283610159","首充","1000",1,"server001",listener);
    }
    //必须调用
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode == KPNetworkData.LOGIN_REQUEST){
            if(resultCode == KPNetworkData.LOGIN_SUCCESS){//登录成功
                KPUserData userData = data.getParcelableExtra(KPNetworkData.USER_CODE);
                Log.e("kp"," userData is "+userData.toString());
                KPGameSdk.showFloatView(this);
            }else if(resultCode == KPNetworkData.RESET_PASSWORD_SUCCESS){

            }
        } else {
            KPGameSdk.checkFloatPermissionOnActivityResult(this,requestCode,requestCode,data);//检测悬浮窗权限是否开启
        }
    }

    public void onDestroy(){
        super.onDestroy();
        KPGameSdk.hideFloatView(this);//在onDestroy中调用
    }

    public void onStart(){
        super.onStart();
        if(KPGameSdk.isLogin(this) && KPGameSdk.checkFloatPermission1(this)){//判断是否登录过
            KPGameSdk.showFloatView2(this);//启动悬浮框
        }
    }

    public void onStop(){
        super.onStop();
        KPGameSdk.hideFloatView(this);
    }

}
